home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / bin / find2perl < prev    next >
Encoding:
Text File  |  2012-12-11  |  23.0 KB  |  880 lines

  1. #!/usr/bin/perl
  2.     eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  3.       if $running_under_some_shell;
  4. (my $perlpath = <<'/../') =~ s/\s*\z//;
  5. /usr/bin/perl
  6. /../
  7. use strict;
  8. use vars qw/$statdone/;
  9. use File::Spec::Functions 'curdir';
  10. my $startperl = "#! $perlpath -w";
  11.  
  12. sub tab ();
  13. sub n ($$);
  14. sub fileglob_to_re ($);
  15. sub quote ($);
  16.  
  17. my @roots = ();
  18. while ($ARGV[0] =~ /^[^-!(]/) {
  19.     push(@roots, shift);
  20. }
  21. @roots = (curdir()) unless @roots;
  22. for (@roots) { $_ = quote($_) }
  23. my $roots = join(', ', @roots);
  24.  
  25. my $find = "find";
  26. my $indent_depth = 1;
  27. my $stat = 'lstat';
  28. my $decl = '';
  29. my $flushall = '';
  30. my $initfile = '';
  31. my $initnewer = '';
  32. my $out = '';
  33. my $declaresubs = "sub wanted;\n";
  34. my %init = ();
  35. my ($follow_in_effect,$Skip_And) = (0,0);
  36. my $print_needed = 1;
  37.  
  38. while (@ARGV) {
  39.     $_ = shift;
  40.     s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n";
  41.     if ($_ eq '(') {
  42.         $out .= tab . "(\n";
  43.         $indent_depth++;
  44.         next;
  45.     } elsif ($_ eq ')') {
  46.         --$indent_depth;
  47.         $out .= tab . ")";
  48.     } elsif ($_ eq 'follow') {
  49.         $follow_in_effect= 1;
  50.         $stat = 'stat';
  51.         $Skip_And= 1;
  52.     } elsif ($_ eq '!') {
  53.         $out .= tab . "!";
  54.         next;
  55.     } elsif (/^(i)?name$/) {
  56.         $out .= tab . '/' . fileglob_to_re(shift) . "/s$1";
  57.     } elsif (/^(i)?path$/) {
  58.         $out .= tab . '$File::Find::name =~ /' . fileglob_to_re(shift) . "/s$1";
  59.     } elsif ($_ eq 'perm') {
  60.         my $onum = shift;
  61.         $onum =~ /^-?[0-7]+$/
  62.             || die "Malformed -perm argument: $onum\n";
  63.         $out .= tab;
  64.         if ($onum =~ s/^-//) {
  65.             $onum = sprintf("0%o", oct($onum) & 07777);
  66.             $out .= "((\$mode & $onum) == $onum)";
  67.         } else {
  68.             $onum =~ s/^0*/0/;
  69.             $out .= "((\$mode & 0777) == $onum)";
  70.         }
  71.     } elsif ($_ eq 'type') {
  72.         (my $filetest = shift) =~ tr/s/S/;
  73.         $out .= tab . "-$filetest _";
  74.     } elsif ($_ eq 'print') {
  75.         $out .= tab . 'print("$name\n")';
  76.     $print_needed = 0;
  77.     } elsif ($_ eq 'print0') {
  78.         $out .= tab . 'print("$name\0")';
  79.     $print_needed = 0;
  80.     } elsif ($_ eq 'fstype') {
  81.         my $type = shift;
  82.         $out .= tab;
  83.         if ($type eq 'nfs') {
  84.             $out .= '($dev < 0)';
  85.         } else {
  86.             $out .= '($dev >= 0)'; #XXX
  87.         }
  88.     } elsif ($_ eq 'user') {
  89.         my $uname = shift;
  90.         $out .= tab . "(\$uid == \$uid{'$uname'})";
  91.         $init{user} = 1;
  92.     } elsif ($_ eq 'group') {
  93.         my $gname = shift;
  94.         $out .= tab . "(\$gid == \$gid{'$gname'})";
  95.         $init{group} = 1;
  96.     } elsif ($_ eq 'nouser') {
  97.         $out .= tab . '!exists $uid{$uid}';
  98.         $init{user} = 1;
  99.     } elsif ($_ eq 'nogroup') {
  100.         $out .= tab . '!exists $gid{$gid}';
  101.         $init{group} = 1;
  102.     } elsif ($_ eq 'links') {
  103.         $out .= tab . n('$nlink', shift);
  104.     } elsif ($_ eq 'inum') {
  105.         $out .= tab . n('$ino', shift);
  106.     } elsif ($_ eq 'size') {
  107.         $_ = shift;
  108.         my $n = 'int(((-s _) + 511) / 512)';
  109.         if (s/c\z//) {
  110.             $n = 'int(-s _)';
  111.         } elsif (s/k\z//) {
  112.             $n = 'int(((-s _) + 1023) / 1024)';
  113.         }
  114.         $out .= tab . n($n, $_);
  115.     } elsif ($_ eq 'atime') {
  116.         $out .= tab . n('int(-A _)', shift);
  117.     } elsif ($_ eq 'mtime') {
  118.         $out .= tab . n('int(-M _)', shift);
  119.     } elsif ($_ eq 'ctime') {
  120.         $out .= tab . n('int(-C _)', shift);
  121.     } elsif ($_ eq 'exec') {
  122.         my @cmd = ();
  123.         while (@ARGV && $ARGV[0] ne ';')
  124.             { push(@cmd, shift) }
  125.         shift;
  126.         $out .= tab;
  127.         if ($cmd[0] =~m#^(?:(?:/usr)?/bin/)?rm$#
  128.                 && $cmd[$#cmd] eq '{}'
  129.                 && (@cmd == 2 || (@cmd == 3 && $cmd[1] eq '-f'))) {
  130.             if (@cmd == 2) {
  131.                 $out .= '(unlink($_) || warn "$name: $!\n")';
  132.             } elsif (!@ARGV) {
  133.                 $out .= 'unlink($_)';
  134.             } else {
  135.                 $out .= '(unlink($_) || 1)';
  136.             }
  137.         } else {
  138.             for (@cmd)
  139.                 { s/'/\\'/g }
  140.             { local $" = "','"; $out .= "doexec(0, '@cmd')"; }
  141.             $declaresubs .= "sub doexec (\$\@);\n";
  142.             $init{doexec} = 1;
  143.         }
  144.     $print_needed = 0;
  145.     } elsif ($_ eq 'ok') {
  146.         my @cmd = ();
  147.         while (@ARGV && $ARGV[0] ne ';')
  148.             { push(@cmd, shift) }
  149.         shift;
  150.         $out .= tab;
  151.         for (@cmd)
  152.             { s/'/\\'/g }
  153.         { local $" = "','"; $out .= "doexec(1, '@cmd')"; }
  154.         $declaresubs .= "sub doexec (\$\@);\n";
  155.         $init{doexec} = 1;
  156.     $print_needed = 0;
  157.     } elsif ($_ eq 'prune') {
  158.         $out .= tab . '($File::Find::prune = 1)';
  159.     } elsif ($_ eq 'xdev') {
  160.         $out .= tab . '!($File::Find::prune |= ($dev != $File::Find::topdev))'
  161. ;
  162.     } elsif ($_ eq 'newer') {
  163.         my $file = shift;
  164.         my $newername = 'AGE_OF' . $file;
  165.         $newername =~ s/\W/_/g;
  166.         $newername = '$' . $newername;
  167.         $out .= tab . "(-M _ < $newername)";
  168.         $initnewer .= "my $newername = -M " . quote($file) . ";\n";
  169.     } elsif ($_ eq 'eval') {
  170.         my $prog = shift;
  171.         $prog =~ s/'/\\'/g;
  172.         $out .= tab . "eval {$prog}";
  173.     $print_needed = 0;
  174.     } elsif ($_ eq 'depth') {
  175.         $find = 'finddepth';
  176.         next;
  177.     } elsif ($_ eq 'ls') {
  178.         $out .= tab . "ls";
  179.         $declaresubs .= "sub ls ();\n";
  180.         $init{ls} = 1;
  181.     $print_needed = 0;
  182.     } elsif ($_ eq 'tar') {
  183.         die "-tar must have a filename argument\n" unless @ARGV;
  184.         my $file = shift;
  185.         my $fh = 'FH' . $file;
  186.         $fh =~ s/\W/_/g;
  187.         $out .= tab . "tar(*$fh, \$name)";
  188.         $flushall .= "tflushall;\n";
  189.         $declaresubs .= "sub tar;\nsub tflushall ();\n";
  190.         $initfile .= "open($fh, " . quote('> ' . $file) .
  191.                      qq{) || die "Can't open $fh: \$!\\n";\n};
  192.         $init{tar} = 1;
  193.     } elsif (/^(n?)cpio\z/) {
  194.         die "-$_ must have a filename argument\n" unless @ARGV;
  195.         my $file = shift;
  196.         my $fh = 'FH' . $file;
  197.         $fh =~ s/\W/_/g;
  198.         $out .= tab . "cpio(*$fh, \$name, '$1')";
  199.         $find = 'finddepth';
  200.         $flushall .= "cflushall;\n";
  201.         $declaresubs .= "sub cpio;\nsub cflushall ();\n";
  202.         $initfile .= "open($fh, " . quote('> ' . $file) .
  203.                      qq{) || die "Can't open $fh: \$!\\n";\n};
  204.         $init{cpio} = 1;
  205.     } else {
  206.         die "Unrecognized switch: -$_\n";
  207.     }
  208.  
  209.     if (@ARGV) {
  210.         if ($ARGV[0] eq '-o') {
  211.             { local($statdone) = 1; $out .= "\n" . tab . "||\n"; }
  212.             $statdone = 0 if $indent_depth == 1 && exists $init{delayedstat};
  213.             $init{saw_or} = 1;
  214.             shift;
  215.         } else {
  216.             $out .= " &&" unless $Skip_And || $ARGV[0] eq ')';
  217.             $out .= "\n";
  218.             shift if $ARGV[0] eq '-a';
  219.         }
  220.     }
  221. }
  222.  
  223. if ($print_needed) {
  224.     my $t = tab;
  225.     if ($t !~ /&&\s*$/) { $t .= '&& ' }
  226.     $out .= "\n" . $t . 'print("$name\n")';
  227. }
  228.  
  229.  
  230. print <<"END";
  231. $startperl
  232.     eval 'exec $perlpath -S \$0 \${1+"\$@"}'
  233.         if 0; #\$running_under_some_shell
  234.  
  235. use strict;
  236. use File::Find ();
  237.  
  238. # Set the variable \$File::Find::dont_use_nlink if you're using AFS,
  239. # since AFS cheats.
  240.  
  241. # for the convenience of &wanted calls, including -eval statements:
  242. use vars qw/*name *dir *prune/;
  243. *name   = *File::Find::name;
  244. *dir    = *File::Find::dir;
  245. *prune  = *File::Find::prune;
  246.  
  247. $declaresubs
  248.  
  249. END
  250.  
  251. if (exists $init{doexec}) {
  252.     print <<'END';
  253. use Cwd ();
  254. my $cwd = Cwd::cwd();
  255.  
  256. END
  257. }  
  258.  
  259. if (exists $init{ls}) {
  260.     print <<'END';
  261. my @rwx = qw(--- --x -w- -wx r-- r-x rw- rwx);
  262. my @moname = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
  263.  
  264. END
  265. }
  266.  
  267. if (exists $init{user} || exists $init{ls} || exists $init{tar}) {
  268.     print "my (%uid, %user);\n";
  269.     print "while (my (\$name, \$pw, \$uid) = getpwent) {\n";
  270.     print '    $uid{$name} = $uid{$uid} = $uid;', "\n"
  271.         if exists $init{user};
  272.     print '    $user{$uid} = $name unless exists $user{$uid};', "\n"
  273.         if exists $init{ls} || exists $init{tar};
  274.     print "}\n\n";
  275. }
  276.  
  277. if (exists $init{group} || exists $init{ls} || exists $init{tar}) {
  278.     print "my (%gid, %group);\n";
  279.     print "while (my (\$name, \$pw, \$gid) = getgrent) {\n";
  280.     print '    $gid{$name} = $gid{$gid} = $gid;', "\n"
  281.         if exists $init{group};
  282.     print '    $group{$gid} = $name unless exists $group{$gid};', "\n"
  283.         if exists $init{ls} || exists $init{tar};
  284.     print "}\n\n";
  285. }
  286.  
  287. print $initnewer, "\n" if $initnewer ne '';
  288. print $initfile, "\n" if $initfile ne '';
  289. $flushall .= "exit;\n";
  290. if (exists $init{declarestat}) {
  291.     $out = <<'END' . $out;
  292.     my ($dev,$ino,$mode,$nlink,$uid,$gid);
  293.  
  294. END
  295. }
  296.  
  297. if ( $follow_in_effect ) {
  298. $out =~ s/lstat\(\$_\)/lstat(_)/;
  299. print <<"END";
  300. $decl
  301. # Traverse desired filesystems
  302. File::Find::$find( {wanted => \\&wanted, follow => 1}, $roots);
  303. $flushall
  304.  
  305. sub wanted {
  306. $out;
  307. }
  308.  
  309. END
  310. } else {
  311. print <<"END";
  312. $decl
  313. # Traverse desired filesystems
  314. File::Find::$find({wanted => \\&wanted}, $roots);
  315. $flushall
  316.  
  317. sub wanted {
  318. $out;
  319. }
  320.  
  321. END
  322. }
  323.  
  324. if (exists $init{doexec}) {
  325.     print <<'END';
  326.  
  327. sub doexec ($@) {
  328.     my $ok = shift;
  329.     my @command = @_; # copy so we don't try to s/// aliases to constants
  330.     for my $word (@command)
  331.         { $word =~ s#{}#$name#g }
  332.     if ($ok) {
  333.         my $old = select(STDOUT);
  334.         $| = 1;
  335.         print "@command";
  336.         select($old);
  337.         return 0 unless <STDIN> =~ /^y/;
  338.     }
  339.     chdir $cwd; #sigh
  340.     system @command;
  341.     chdir $File::Find::dir;
  342.     return !$?;
  343. }
  344.  
  345. END
  346. }
  347.  
  348. if (exists $init{ls}) {
  349.     print <<'INTRO', <<"SUB", <<'END';
  350.  
  351. sub sizemm {
  352.     my $rdev = shift;
  353.     sprintf("%3d, %3d", ($rdev >> 8) & 0xff, $rdev & 0xff);
  354. }
  355.  
  356. sub ls () {
  357.     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  358. INTRO
  359.         \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_);
  360. SUB
  361.     my $pname = $name;
  362.  
  363.     $blocks
  364.         or $blocks = int(($size + 1023) / 1024);
  365.  
  366.     my $perms = $rwx[$mode & 7];
  367.     $mode >>= 3;
  368.     $perms = $rwx[$mode & 7] . $perms;
  369.     $mode >>= 3;
  370.     $perms = $rwx[$mode & 7] . $perms;
  371.     substr($perms, 2, 1) =~ tr/-x/Ss/ if -u _;
  372.     substr($perms, 5, 1) =~ tr/-x/Ss/ if -g _;
  373.     substr($perms, 8, 1) =~ tr/-x/Tt/ if -k _;
  374.     if    (-f _) { $perms = '-' . $perms; }
  375.     elsif (-d _) { $perms = 'd' . $perms; }
  376.     elsif (-l _) { $perms = 'l' . $perms; $pname .= ' -> ' . readlink($_); }
  377.     elsif (-c _) { $perms = 'c' . $perms; $size = sizemm($rdev); }
  378.     elsif (-b _) { $perms = 'b' . $perms; $size = sizemm($rdev); }
  379.     elsif (-p _) { $perms = 'p' . $perms; }
  380.     elsif (-S _) { $perms = 's' . $perms; }
  381.     else         { $perms = '?' . $perms; }
  382.  
  383.     my $user = $user{$uid} || $uid;
  384.     my $group = $group{$gid} || $gid;
  385.  
  386.     my ($sec,$min,$hour,$mday,$mon,$timeyear) = localtime($mtime);
  387.     if (-M _ > 365.25 / 2) {
  388.         $timeyear += 1900;
  389.     } else {
  390.         $timeyear = sprintf("%02d:%02d", $hour, $min);
  391.     }
  392.  
  393.     printf "%5lu %4ld %-10s %3d %-8s %-8s %8s %s %2d %5s %s\n",
  394.             $ino,
  395.                  $blocks,
  396.                       $perms,
  397.                             $nlink,
  398.                                 $user,
  399.                                      $group,
  400.                                           $size,
  401.                                               $moname[$mon],
  402.                                                  $mday,
  403.                                                      $timeyear,
  404.                                                          $pname;
  405.     1;
  406. }
  407.  
  408. END
  409. }
  410.  
  411.  
  412. if (exists $init{cpio} || exists $init{tar}) {
  413. print <<'END';
  414.  
  415. my %blocks = ();
  416.  
  417. sub flush {
  418.     my ($fh, $varref, $blksz) = @_;
  419.  
  420.     while (length($$varref) >= $blksz) {
  421.         no strict qw/refs/;
  422.         syswrite($fh, $$varref, $blksz);
  423.         substr($$varref, 0, $blksz) = '';
  424.         ++$blocks{$fh};
  425.     }
  426. }
  427.  
  428. END
  429. }
  430.  
  431.  
  432. if (exists $init{cpio}) {
  433.     print <<'INTRO', <<"SUB", <<'END';
  434.  
  435. my %cpout = ();
  436. my %nc = ();
  437.  
  438. sub cpio {
  439.     my ($fh, $fname, $nc) = @_;
  440.     my $text = '';
  441.     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  442.         $atime,$mtime,$ctime,$blksize,$blocks);
  443.     local (*IN);
  444.  
  445.     if ( ! defined $fname ) {
  446.         $fname = 'TRAILER!!!';
  447.         ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  448.           $atime,$mtime,$ctime,$blksize,$blocks) = (0) x 13;
  449.     } else {
  450.         ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  451. INTRO
  452.           \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_);
  453. SUB
  454.         if (-f _) {
  455.             open(IN, "./$_\0") || do {
  456.                 warn "Couldn't open $fname: $!\n";
  457.                 return;
  458.             }
  459.         } else {
  460.             $text = readlink($_);
  461.             $size = 0 unless defined $text;
  462.         }
  463.     }
  464.  
  465.     $fname =~ s#^\./##;
  466.     $nc{$fh} = $nc;
  467.     if ($nc eq 'n') {
  468.         $cpout{$fh} .=
  469.           sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
  470.             070707,
  471.             $dev & 0777777,
  472.             $ino & 0777777,
  473.             $mode & 0777777,
  474.             $uid & 0777777,
  475.             $gid & 0777777,
  476.             $nlink & 0777777,
  477.             $rdev & 0177777,
  478.             $mtime,
  479.             length($fname)+1,
  480.             $size,
  481.             $fname);
  482.     } else {
  483.         $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1;
  484.         $cpout{$fh} .= pack("SSSSSSSSLSLa*",
  485.             070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime,
  486.             length($fname)+1, $size,
  487.             $fname . (length($fname) & 1 ? "\0" : "\0\0"));
  488.     }
  489.  
  490.     if ($text ne '') {
  491.         $cpout{$fh} .= $text;
  492.     } elsif ($size) {
  493.         my $l;
  494.         flush($fh, \$cpout{$fh}, 5120)
  495.             while ($l = length($cpout{$fh})) >= 5120;
  496.         while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
  497.             flush($fh, \$cpout{$fh}, 5120);
  498.             $l = length($cpout{$fh});
  499.         }
  500.         close IN;
  501.     }
  502. }
  503.  
  504. sub cflushall () {
  505.     for my $fh (keys %cpout) {
  506.         cpio($fh, undef, $nc{$fh});
  507.         $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
  508.         flush($fh, \$cpout{$fh}, 5120);
  509.         print $blocks{$fh} * 10, " blocks\n";
  510.     }
  511. }
  512.  
  513. END
  514. }
  515.  
  516. if (exists $init{tar}) {
  517.     print <<'INTRO', <<"SUB", <<'END';
  518.  
  519. my %tarout = ();
  520. my %linkseen = ();
  521.  
  522. sub tar {
  523.     my ($fh, $fname) = @_;
  524.     my $prefix = '';
  525.     my $typeflag = '0';
  526.     my $linkname;
  527.     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  528. INTRO
  529.         \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_);
  530. SUB
  531.     local (*IN);
  532.  
  533.     if ($nlink > 1) {
  534.         if ($linkname = $linkseen{$fh, $dev, $ino}) {
  535.             if (length($linkname) > 100) {
  536.                 warn "$0: omitting file with linkname ",
  537.                      "too long for tar output: $linkname\n";
  538.                 return;
  539.             }
  540.             $typeflag = '1';
  541.             $size = 0;
  542.         } else {
  543.             $linkseen{$fh, $dev, $ino} = $fname;
  544.         }
  545.     }
  546.     if ($typeflag eq '0') {
  547.         if (-f _) {
  548.             open(IN, "./$_\0") || do {
  549.                 warn "Couldn't open $fname: $!\n";
  550.                 return;
  551.             }
  552.         } else {
  553.             $linkname = readlink($_);
  554.             if (defined $linkname) { $typeflag = '2' }
  555.             elsif (-c _) { $typeflag = '3' }
  556.             elsif (-b _) { $typeflag = '4' }
  557.             elsif (-d _) { $typeflag = '5' }
  558.             elsif (-p _) { $typeflag = '6' }
  559.         }
  560.     }
  561.  
  562.     if (length($fname) > 100) {
  563.         ($prefix, $fname) = ($fname =~ m#\A(.*?)/(.{,100})\Z(?!\n)#);
  564.         if (!defined($fname) || length($prefix) > 155) {
  565.             warn "$0: omitting file with name too long for tar output: ",
  566.                  $fname, "\n";
  567.             return;
  568.         }
  569.     }
  570.  
  571.     $size = 0 if $typeflag ne '0';
  572.     my $header = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155",
  573.                         $fname,
  574.                         sprintf("%7o ", $mode &    0777),
  575.                         sprintf("%7o ", $uid  & 0777777),
  576.                         sprintf("%7o ", $gid  & 0777777),
  577.                         sprintf("%11o ", $size),
  578.                         sprintf("%11o ", $mtime),
  579.                         ' 'x8,
  580.                         $typeflag,
  581.                         defined $linkname ? $linkname : '',
  582.                         "ustar\0",
  583.                         "00",
  584.                         $user{$uid},
  585.                         $group{$gid},
  586.                         ($rdev >> 8) & 0xff,
  587.                         $rdev & 0xff,
  588.                         $prefix,
  589.                      );
  590.     substr($header, 148, 8) = sprintf("%7o ", unpack("%16C*", $header));
  591.     my $l = length($header) % 512;
  592.     $tarout{$fh} .= $header;
  593.     $tarout{$fh} .= "\0" x (512 - $l) if $l;
  594.  
  595.     if ($size) {
  596.         flush($fh, \$tarout{$fh}, 10240)
  597.             while ($l = length($tarout{$fh})) >= 10240;
  598.         while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
  599.             my $slop = length($tarout{$fh}) % 512;
  600.             $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
  601.             flush($fh, \$tarout{$fh}, 10240);
  602.             $l = length($tarout{$fh});
  603.         }
  604.         close IN;
  605.     }
  606. }
  607.  
  608. sub tflushall () {
  609.     my $len;
  610.     for my $fh (keys %tarout) {
  611.         $len = 10240 - length($tarout{$fh});
  612.         $len += 10240 if $len < 1024;
  613.         $tarout{$fh} .= "\0" x $len;
  614.         flush($fh, \$tarout{$fh}, 10240);
  615.     }
  616. }
  617.  
  618. END
  619. }
  620.  
  621. exit;
  622.  
  623. ############################################################################
  624.  
  625. sub tab () {
  626.     my $tabstring;
  627.  
  628.     $tabstring = "\t" x ($indent_depth/2) . ' ' x ($indent_depth%2 * 4);
  629.     if (!$statdone) {
  630.         if ($_ =~ /^(?:name|print|prune|exec|ok|\(|\))/) {
  631.             $init{delayedstat} = 1;
  632.         } else {
  633.             my $statcall = '(($dev,$ino,$mode,$nlink,$uid,$gid) = '
  634.                          . $stat . '($_))';
  635.             if (exists $init{saw_or}) {
  636.                 $tabstring .= "(\$nlink || $statcall) &&\n" . $tabstring;
  637.             } else {
  638.                 $tabstring .= "$statcall &&\n" . $tabstring;
  639.             }
  640.             $statdone = 1;
  641.             $init{declarestat} = 1;
  642.         }
  643.     }
  644.     $tabstring =~ s/^\s+/ / if $out =~ /!$/;
  645.     $tabstring;
  646. }
  647.  
  648. sub fileglob_to_re ($) {
  649.     my $x = shift;
  650.     $x =~ s#([./^\$()+])#\\$1#g;
  651.     $x =~ s#([?*])#.$1#g;
  652.     "^$x\\z";
  653. }
  654.  
  655. sub n ($$) {
  656.     my ($pre, $n) = @_;
  657.     $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /;
  658.     $n =~ s/ 0*(\d)/ $1/;
  659.     "($pre $n)";
  660. }
  661.  
  662. sub quote ($) {
  663.     my $string = shift;
  664.     $string =~ s/\\/\\\\/g;
  665.     $string =~ s/'/\\'/g;
  666.     "'$string'";
  667. }
  668.  
  669. __END__
  670.  
  671. =head1 NAME
  672.  
  673. find2perl - translate find command lines to Perl code
  674.  
  675. =head1 SYNOPSIS
  676.  
  677.     find2perl [paths] [predicates] | perl
  678.  
  679. =head1 DESCRIPTION
  680.  
  681. find2perl is a little translator to convert find command lines to
  682. equivalent Perl code.  The resulting code is typically faster than
  683. running find itself.
  684.  
  685. "paths" are a set of paths where find2perl will start its searches and
  686. "predicates" are taken from the following list.
  687.  
  688. =over 4
  689.  
  690. =item C<! PREDICATE>
  691.  
  692. Negate the sense of the following predicate.  The C<!> must be passed as
  693. a distinct argument, so it may need to be surrounded by whitespace and/or
  694. quoted from interpretation by the shell using a backslash (just as with
  695. using C<find(1)>).
  696.  
  697. =item C<( PREDICATES )>
  698.  
  699. Group the given PREDICATES.  The parentheses must be passed as distinct
  700. arguments, so they may need to be surrounded by whitespace and/or
  701. quoted from interpretation by the shell using a backslash (just as with
  702. using C<find(1)>).
  703.  
  704. =item C<PREDICATE1 PREDICATE2>
  705.  
  706. True if _both_ PREDICATE1 and PREDICATE2 are true; PREDICATE2 is not
  707. evaluated if PREDICATE1 is false.
  708.  
  709. =item C<PREDICATE1 -o PREDICATE2>
  710.  
  711. True if either one of PREDICATE1 or PREDICATE2 is true; PREDICATE2 is
  712. not evaluated if PREDICATE1 is true.
  713.  
  714. =item C<-follow>
  715.  
  716. Follow (dereference) symlinks.  The checking of file attributes depends
  717. on the position of the C<-follow> option. If it precedes the file
  718. check option, an C<stat> is done which means the file check applies to the
  719. file the symbolic link is pointing to. If C<-follow> option follows the
  720. file check option, this now applies to the symbolic link itself, i.e.
  721. an C<lstat> is done.
  722.  
  723. =item C<-depth>
  724.  
  725. Change directory traversal algorithm from breadth-first to depth-first.
  726.  
  727. =item C<-prune>
  728.  
  729. Do not descend into the directory currently matched.
  730.  
  731. =item C<-xdev>
  732.  
  733. Do not traverse mount points (prunes search at mount-point directories).
  734.  
  735. =item C<-name GLOB>
  736.  
  737. File name matches specified GLOB wildcard pattern.  GLOB may need to be
  738. quoted to avoid interpretation by the shell (just as with using
  739. C<find(1)>).
  740.  
  741. =item C<-iname GLOB>
  742.  
  743. Like C<-name>, but the match is case insensitive.
  744.  
  745. =item C<-path GLOB>
  746.  
  747. Path name matches specified GLOB wildcard pattern.
  748.  
  749. =item C<-ipath GLOB>
  750.  
  751. Like C<-path>, but the match is case insensitive.
  752.  
  753. =item C<-perm PERM>
  754.  
  755. Low-order 9 bits of permission match octal value PERM.
  756.  
  757. =item C<-perm -PERM>
  758.  
  759. The bits specified in PERM are all set in file's permissions.
  760.  
  761. =item C<-type X>
  762.  
  763. The file's type matches perl's C<-X> operator.
  764.  
  765. =item C<-fstype TYPE>
  766.  
  767. Filesystem of current path is of type TYPE (only NFS/non-NFS distinction
  768. is implemented).
  769.  
  770. =item C<-user USER>
  771.  
  772. True if USER is owner of file.
  773.  
  774. =item C<-group GROUP>
  775.  
  776. True if file's group is GROUP.
  777.  
  778. =item C<-nouser>
  779.  
  780. True if file's owner is not in password database.
  781.  
  782. =item C<-nogroup>
  783.  
  784. True if file's group is not in group database.
  785.  
  786. =item C<-inum INUM>
  787.  
  788. True file's inode number is INUM.
  789.  
  790. =item C<-links N>
  791.  
  792. True if (hard) link count of file matches N (see below).
  793.  
  794. =item C<-size N>
  795.  
  796. True if file's size matches N (see below) N is normally counted in
  797. 512-byte blocks, but a suffix of "c" specifies that size should be
  798. counted in characters (bytes) and a suffix of "k" specifies that
  799. size should be counted in 1024-byte blocks.
  800.  
  801. =item C<-atime N>
  802.  
  803. True if last-access time of file matches N (measured in days) (see
  804. below).
  805.  
  806. =item C<-ctime N>
  807.  
  808. True if last-changed time of file's inode matches N (measured in days,
  809. see below).
  810.  
  811. =item C<-mtime N>
  812.  
  813. True if last-modified time of file matches N (measured in days, see below).
  814.  
  815. =item C<-newer FILE>
  816.  
  817. True if last-modified time of file matches N.
  818.  
  819. =item C<-print>
  820.  
  821. Print out path of file (always true). If none of C<-exec>, C<-ls>,
  822. C<-print0>, or C<-ok> is specified, then C<-print> will be added
  823. implicitly.
  824.  
  825. =item C<-print0>
  826.  
  827. Like -print, but terminates with \0 instead of \n.
  828.  
  829. =item C<-exec OPTIONS ;>
  830.  
  831. exec() the arguments in OPTIONS in a subprocess; any occurrence of {} in
  832. OPTIONS will first be substituted with the path of the current
  833. file.  Note that the command "rm" has been special-cased to use perl's
  834. unlink() function instead (as an optimization).  The C<;> must be passed as
  835. a distinct argument, so it may need to be surrounded by whitespace and/or
  836. quoted from interpretation by the shell using a backslash (just as with
  837. using C<find(1)>).
  838.  
  839. =item C<-ok OPTIONS ;>
  840.  
  841. Like -exec, but first prompts user; if user's response does not begin
  842. with a y, skip the exec.  The C<;> must be passed as
  843. a distinct argument, so it may need to be surrounded by whitespace and/or
  844. quoted from interpretation by the shell using a backslash (just as with
  845. using C<find(1)>).
  846.  
  847. =item C<-eval EXPR>
  848.  
  849. Has the perl script eval() the EXPR.  
  850.  
  851. =item C<-ls>
  852.  
  853. Simulates C<-exec ls -dils {} ;>
  854.  
  855. =item C<-tar FILE>
  856.  
  857. Adds current output to tar-format FILE.
  858.  
  859. =item C<-cpio FILE>
  860.  
  861. Adds current output to old-style cpio-format FILE.
  862.  
  863. =item C<-ncpio FILE>
  864.  
  865. Adds current output to "new"-style cpio-format FILE.
  866.  
  867. =back
  868.  
  869. Predicates which take a numeric argument N can come in three forms:
  870.  
  871.    * N is prefixed with a +: match values greater than N
  872.    * N is prefixed with a -: match values less than N
  873.    * N is not prefixed with either + or -: match only values equal to N
  874.  
  875. =head1 SEE ALSO
  876.  
  877. find, File::Find.
  878.  
  879. =cut
  880.